package io.renren.modules.back.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.back.entity.AdEntity;
import io.renren.modules.back.service.AdService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 广告api
 *
 * @author zzh
 * @email 862583291@qq.com
 * @date 2019年5月11日 上午10:40:10
 */
@RestController
@RequestMapping("/back/ad")
public class AdController {
    @Autowired
    private AdService adService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("ad:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<AdEntity> adList = adService.queryList(query);
        int total = adService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(adList, total, query.getLimit(), query.getCurrPage());
        return R.ok().put("data", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("ad:info")
    public R info(@PathVariable("id") Long id) {
        AdEntity shop = adService.getById(id);
        return R.ok().put("data", shop);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("ad:save")
    public R save(@RequestBody AdEntity ad) {
        ValidatorUtils.validateEntity(ad, AddGroup.class);
        adService.save(ad);
        return R.ok("执行成功");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("ad:update")
    public R update(@RequestBody AdEntity ad) {
        ValidatorUtils.validateEntity(ad, UpdateGroup.class);
        adService.updateById(ad);
        return R.ok("执行成功");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("ad:delete")
    public R delete(@RequestBody Long[] ids) {
        adService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
